/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package utils;

/**
 *
 * @author Tavo
 */
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.component.UIInput;
import javax.faces.context.FacesContext;

public class Validaciones
{
    public void validarEmail(FacesContext context, UIComponent toValidate,Object valor)
    {
    String email = (String) valor;

    if (email.indexOf('@') == -1 || email.equals(""))
    {
       ((UIInput)toValidate).setValid(false);

        FacesMessage message = new FacesMessage("*No Valido");
        context.addMessage(toValidate.getClientId(context), message);
    }
    }


    public void validarPassword(FacesContext context, UIComponent toValidate,Object valor)
    {
    String pass = (String) valor;

    if (pass.length()==0)
    {
       ((UIInput)toValidate).setValid(false);

        FacesMessage message = new FacesMessage("No Valido");
        context.addMessage(toValidate.getClientId(context), message);
    }
    }

    public void validarNombre(FacesContext context, UIComponent toValidate,Object valor){

        String pass = (String) valor;

        if (pass.length()==0){
            ((UIInput)toValidate).setValid(false);
            FacesMessage message = new FacesMessage("*No Valido");
            context.addMessage(toValidate.getClientId(context), message);
        
        }
        else{
            Pattern p = Pattern.compile("^[A-Za-z\\s]+$");
            Matcher m = p.matcher(pass);
            if (!m.find()){
                ((UIInput)toValidate).setValid(false);
                FacesMessage message = new FacesMessage("*No válido");
                context.addMessage(toValidate.getClientId(context), message);
            }
        }
    }

    public void validarNombreABuscar(FacesContext context, UIComponent toValidate,Object valor){

        String nombre = (String) valor;

        if (nombre.length()==0){
            ((UIInput)toValidate).setValid(false);
            FacesMessage message = new FacesMessage("*No Valido");
            context.addMessage(toValidate.getClientId(context), message);
        }
        else{
            Pattern p = Pattern.compile("^[A-Za-z\\s]+$");
            Matcher m = p.matcher(nombre);
            if (!m.find()){
                ((UIInput)toValidate).setValid(false);
                FacesMessage message = new FacesMessage("*No válido");
                context.addMessage(toValidate.getClientId(context), message);
            }
        }
    }
}
